package com.sheng.project.demo.crypt;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.sheng.project.demo.utils.Sm2Util;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

import java.math.BigInteger;
import java.security.KeyPair;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class GenerateKeyPair {

    public static void generateKeyPair() {
        KeyPair kp1 = Sm2Util.generateKeyPair();
        //       这里的d请三方保存，用于后续的解密中行加密数据
        BigInteger d = ((BCECPrivateKey) kp1.getPrivate()).getD();
        System.out.println("三方的私钥生成坐标d，三方需保留，用于私钥解密:" + d);
        byte[] encoded = ((ECPublicKey) kp1.getPublic()).getQ().getEncoded(false);
        //        这里生成的公钥需要给到中行，用于中行进行sm2加密
        System.out.println("三方给中行的公钥：" + Hex.toHexString(encoded).toUpperCase().substring(2));
        System.out.println(LocalDate.now().format(DateTimeFormatter.ISO_DATE));
    }

    public static void main(String[] args) {
        generateKeyPair();
    }

    @Test
    public void test001() {
        String dateString = "2024-04-13 10:00:00";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            // 将日期字符串解析为 Date 对象
            Date inputDate = sdf.parse(dateString);

            // 获取当前时间
            Date currentDate = new Date();
            long between = DateUtil.between(currentDate, inputDate, DateUnit.DAY, Boolean.FALSE);
            int compare = DateUtil.compare(currentDate, inputDate, "yyyy-MM-dd HH:mm:ss");
            System.out.println(between);
            System.out.println(compare);

            // 比较给定日期与当前日期
//            if (inputDate.before(currentDate) || inputDate.equals(currentDate)) {
//                System.out.println(dateString + " 小于等于当前时间");
//            } else {
//                System.out.println(dateString + " 大于当前时间");
//            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    //三方的私钥生成坐标d，三方需保留，用于私钥解密:101959616850875943932697770166287063999683974043242337818732170373234155890783
    //三方给中行的公钥：A4E89E388358D0023CD4AB54092C5D3ABDDD5C285B5C45D7CC44EDF89166B9D4F24B8703F1414BA850BB48F4FFE9553444585244D2C719D0D59C587BEB63A1EA
}
